package com.cash.lemoloan.statisticlib.Utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


import com.cash.lemoloan.statisticlib.StatisticApplication;

import java.util.Map;
import java.util.Set;

public class PreferencesUtil {
    private static SharedPreferences mPreferences;
    private static String mShareName = "lemonlan";

    private PreferencesUtil() {
    }

    private static SharedPreferences getInstance() {
        if (mPreferences == null) {
            synchronized (PreferencesUtil.class) {
                if (mPreferences == null) {
                    mPreferences = StatisticApplication.getInstance().getSharedPreferences(mShareName, Context.MODE_PRIVATE);
                }
            }
        }
        return mPreferences;
    }

    public static SharedPreferences getInstance(String shareName) {
        if (mPreferences == null) {
            synchronized (PreferencesUtil.class) {
                if (mPreferences == null) {
                    mPreferences = StatisticApplication.getInstance().getSharedPreferences(shareName, Context.MODE_PRIVATE);
                }
            }
        }
        return mPreferences;
    }

    /**
     * 判断是否存在某个键
     * @param key
     * @return
     */
    public static boolean contain(String key){
        return getInstance().contains(key);
    }

    /**
     * 保存string类型数据
     */
    public static boolean putString(String key, String value) {
        Editor editor = getInstance().edit();
        editor.putString(key, value);
        return editor.commit();
    }
    /**
     * 保存float类型数据
     */
    public static boolean putFloat(String key, float value) {
        Editor editor = getInstance().edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    /**
     * 根据key读取参数
     */
    public static String getString(String key) {
        return getInstance().getString(key, "");
    }
    /**
     * 根据key读取参数
     */
    public static float getFloat(String key) {
        return getInstance().getFloat(key,0);
    }

    /**
     * 读取需要转为int类型的String
     *
     * @return
     */
    public static String getStringToInt(String key) {
        return getInstance().getString(key, "0");
    }

    /**
     * 保存long类型数据
     */
    public static boolean putLong(String key, long value) {
        Editor editor = getInstance().edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    /**
     * 获取long类型数据
     */
    public static long getLong(String key) {
        return getInstance().getLong(key, -1);
    }

    /**
     * 保存int类型数据
     *
     * @param
     * @param value
     */
    public static boolean putInt(String key, int value) {
        Editor editor = getInstance().edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    /**
     * 获取int类型数据
     *
     * @param key
     * @return
     */
    public static int getInt(String key) {
        return getInstance().getInt(key, -1);
    }

    /**
     * 读取boolean
     */
    public static boolean getBoolean(String key, boolean def) {
        return getInstance().getBoolean(key, def);
    }

    /**
     * 保存boolean
     */
    public static boolean putBoolean(String key, boolean value) {
        Editor editor = getInstance().edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }


    /**
     * 批量保存
     */
    public static boolean saveInfo(Map<String, String> info) {
        Editor editor = getInstance().edit();
        Set<String> keys = info.keySet();
        for (String str : keys) {
            editor.putString(str, info.get(str));
        }
        return editor.commit();
    }

    /**
     * 清除SP中的某个键值对
     * @param key
     */
    public static void deleteKeyAndValues(String key){
        if(getInstance().contains(key)) {
            getInstance().edit().remove(key).apply();
        }
    }



}
